﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Light.Framework
{
    /// <summary>
    /// 需要修改的项
    /// 如果需要保存的项为空或数量为空时，表示保存全部属性值
    /// 如果保存的项为空或数量为0，则读取不需要保存的项
    /// </summary>
    public class UpdateColumn
    {

        private List<string> includes = null;
        private List<string> excludes = null;

        /// <summary>
        /// 构造函数
        /// 默认更新多对多关联
        /// </summary>
        public UpdateColumn(Type type)
        {
            Dictionary<string, ManyToManyInfo> dic = DbMapper.GetManyToMany(type);
            if (dic != null && dic.Count > 0)
                IsManyToManyUpdate = true;
            else
                IsManyToManyUpdate = false;

            includes = new List<string>();
            excludes = new List<string>();
        }

        /// <summary>
        /// 是否更新多对多关联项
        /// </summary>
        public bool IsManyToManyUpdate { get; set; }

        /// <summary>
        /// 添加需要查询的属性名（当需要保存的为项为0时，默认保存全部属性的值）
        /// </summary>
        /// <param name="propertyName"></param>
        public void Add(string propertyName)
        {
            if (!includes.Contains(propertyName))
                includes.Add(propertyName);
        }


        /// <summary>
        /// 删除不需要保存的属性名
        /// </summary>
        /// <param name="propertyName"></param>
        public void Delete(string propertyName)
        {
            if (!excludes.Contains(propertyName))
                excludes.Add(propertyName);
        }

        /// <summary>
        /// 结果中是否包含指定的属性名
        /// </summary>
        /// <param name="propertyName"></param>
        /// <returns></returns>
        public bool Include(string propertyName)
        {
            if (includes.Count > 0
                && !String.IsNullOrWhiteSpace(propertyName)
                && !includes.Exists(t => t == propertyName || (t.StartsWith(propertyName) && t.Contains("."))))
            {
                return false;
            }
            if (!String.IsNullOrWhiteSpace(propertyName)
                && excludes.Exists(t => t == propertyName || (propertyName.StartsWith(t) && propertyName.Contains("."))))
            {
                return false;
            }
            return true;
        }
    }
}
